﻿namespace Triad.Common
{
    using System;
    using System.Web;

    /// <summary>
    /// Utility Functions for GSFWeb
    /// </summary>
    public class Utility
    {
        /// <summary>
        /// Writes the log.
        /// </summary>
        /// <param name="p_Exception">The exception.</param>
        /// <remarks>
        /// Author: tim.fischer,   5/1/2008 - 3:13 PM
        /// Modified By: 
        /// </remarks>
        public static void WriteLog(Exception p_Exception)
        {
            Elmah.ErrorLog.GetDefault(HttpContext.Current);
        }

        /// <summary>
        /// VB equivalent to IsNumeric
        /// </summary>
        /// <param name="p_Expression">The expression.</param>
        /// <returns>
        /// 	<c>true</c> if the specified expression is numeric; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNumeric(object p_Expression)
        {
            double retNum;
            bool isNum = Double.TryParse(Convert.ToString(p_Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;

        }
        /// <summary>
        /// VB Equivalent to ISDBNull
        /// </summary>
        /// <param name="p_Expression">The expression.</param>
        /// <returns>
        /// 	<c>true</c> if [is DB null] [the specified expression]; otherwise, <c>false</c>.
        /// </returns>

        public static bool IsDBNull(object p_Expression)
        {
            return p_Expression == DBNull.Value;
        }

        /// <summary>
        /// Manage Null / String
        /// </summary>
        /// <param name="p_Object">The object to test.</param>
        /// <returns></returns>
        public static string MNS(object p_Object)
        {
            return p_Object == DBNull.Value ? string.Empty : Convert.ToString(p_Object);
        }

        /// <summary>
        /// Manage Null / Integer
        /// </summary>
        /// <param name="p_Object">The object to test</param>
        /// <returns>Integer</returns>
        public static int MNI(object p_Object)
        {
            return p_Object == DBNull.Value ? 0 : Convert.ToInt32(p_Object);
        }
    }
}
